/**
 * 规则等级1
 * 0,1,2
 * 等级2
 * 0,3,4,5
 * 等级3
 * 0,6,7,8
 * 等级4
 * 0,9,10,11,
 * 等级5
 * 0,12,13
 * 
 * @param {*} level 
 */
var fs = require('fs');
var niuMap = JSON.parse(fs.readFileSync('./app/logic/sub/HSNN/random/random.json'));

var niuHasNumName = [
    "oneNiu",
    "twoNiu",
    "threeNiu",
    "fourNiu",
    "fiveNiu",
    "sixNiu",
    "sevenNiu",
    "eightNiu",
    "nineNiu",
    "niuNiu",
]

function getNiu(level) {
    switch (level) {
        case 1:
            return sum(0, 2);
        case 2:
            return sum(3, 5);

        case 3:
            return sum(5, 7);

        case 4:
            return sum(7, 8);

        case 5:
            return sum(8, 9);

        default:
            return [];
    }
}


function sum(min, max) {
    var getRand = getRandomRange(min, max);
    console.log(getRand)
    var getNiu = niuHasNumName[getRand]
    console.log(getNiu)
    var temp = niuMap[getNiu].slice();
    var getArrayNum = getRandom(temp.length) - 1;
    console.log(getArrayNum);
    var getArray = temp[getArrayNum];
    console.log(getArray);

    for (var i = 0; i < 10; ++i) {
        var getArray = getRandomNiu(getArray);
        if (getArray.length == 0) {
            continue;
        }
        return getArray;
    }

    return [];
}
function checkNiu(cards) {
    var tempCards = cards.slice();
    tempCards.sort();
    if (tempCards[0] == tempCards[4]) {
        return false;
    }
    return true;
}
function getRandomNiu(cards) {
    var i = getRandom(4);
    var j = getRandom(4);
    var k = getRandom(4);
    var l = getRandom(4);
    var m = getRandom(4);

    var temp = [];
    temp.push(cards[0] + (i) * 100);
    temp.push(cards[1] + (j) * 100);
    temp.push(cards[2] + (k) * 100);
    temp.push(cards[3] + (l) * 100);
    temp.push(cards[4] + (m) * 100);

    if (checkNiu(temp)) {
        return temp;
    }

    // for (var i = 0; i < 4; ++i) {
    //     for (var j = 0; j < 4; ++j) {
    //         for (var k = 0; k < 4; ++k) {
    //             for (var l = 0; l < 4; ++l) {
    //                 for (var m = 0; m < 4; ++m) {
    //                     var temp = [];
    //                     temp.push(cards[0] + (i + 1) * 100);
    //                     temp.push(cards[1] + (j + 1) * 100);
    //                     temp.push(cards[2] + (k + 1) * 100);
    //                     temp.push(cards[3] + (l + 1) * 100);
    //                     temp.push(cards[4] + (m + 1) * 100);

    //                     if (checkNiu(temp)) {
    //                         return temp;
    //                     }
    //                 }
    //             }
    //         }
    //     }
    // }
    return [];
}
function getRandom(num) {
    return Math.floor(Math.random() * num) + 1;
}
function getRandomRange(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

module.exports= {
    getNiu:getNiu
}
